<?php
/**
 * Created by PhpStorm.
 * User: Ruzi
 * Date: 2015/12/10
 * Time: 12:55
 */

header("Content-type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");

//地址映射表
function get_ips($host,$community){

    //虚拟接口
    $ipNetToMediaIfIndex=@snmpwalk("$host","$community","ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaIfIndex");
    //mac地址
    $ipNetToMediaPhysAddress=@snmpwalk("$host","$community","ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaPhysAddress");
    //ip地址
    $ipNetToMediaNetAddress=@snmpwalk("$host","$community","ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaNetAddress");
    //类型
    $ipNetToMediaType=@snmpwalk("$host","$community","ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaType");


    $detection_array= array("hdport"=>[],"vlan" =>[] ,"mac"=>[],"ip_address"=>[],"type"=>[]);

    for($i=0;$i<count($ipNetToMediaIfIndex);$i++){
        $vlan=@str_replace("INTEGER:","",$ipNetToMediaIfIndex[$i]);
        $mac=@str_replace("STRING:","",$ipNetToMediaPhysAddress[$i]);
        $ip=@str_replace("IpAddress:","",$ipNetToMediaNetAddress[$i]);
        $type=@str_replace("STRING:","",$ipNetToMediaType[$i]);

        array_push($detection_array["vlan"],trim($vlan));
        array_push($detection_array["mac"],trim($mac));
        array_push($detection_array["ip_address"],trim($ip));
        array_push($detection_array["type"],trim($type));

    }

    return $detection_array;
}


if(isset($_POST["url"])) {
    $url=$_POST["url"];
    $tar1=array();
    $main_arr = array("网络设备" =>$url, "网络设备下的设备信息" => $tar1);
    $d1 = get_ips($url,"public");

    array_push($main_arr["网络设备下的设备信息"], $d1);


    echo json_encode($main_arr);

}